<?php

/**
 * Description of Application_Model_Fields_Select
 *
 * @author Jean Brito
 */
abstract class Application_Model_Fields_Select {

    static protected function standField($field, $setup) {
        foreach ($setup as $i => $v)
            if (!isset($field[$i]))
                $field[$i] = $v;
        return $field;
    }

    static public function getCreateSql($setup) {
        $padrao = Array(
            'sqlType' => 'varchar',
            'sqlSize' => 2,
            'required' => true,
        );
        return self::standField($setup, $padrao);
    }

    static public function getInsertFieldSql($setup, $data) {
        $padrao = Array(
        );
        $setup['value'] = "'" . $data . "'";
        return self::standField($setup, $padrao);
    }

    static public function getStoreField($setup) {
        $padrao = Array(
            'extType' => 'string',
        );

        return self::standField($setup, $padrao);
    }

    static public function getGridColumn($setup) {
        $options='';
        foreach ($setup['options'] as $key => $value)
            $options.="if(value=='$key')return '$value';";
        $padrao = Array(
            'header' => isset($setup['label']) ? $setup['label'] : $setup['name'],
            'dataIndex' => $setup['name'],
            'flex' => 3,
            'extRenderer' => "$options;return ''",
        );
        return self::standField($setup, $padrao);
    }

    static public function getFormInput($setup) {
        $options = "";
        if (isset($setup['options'])) {
            foreach ($setup['options'] as $key => $value) {
                $options.="{key:'$key',value:'$value'},";
            }
        }
        $options = "[" . substr($options, 0, strlen($options) - 1) . "]";
        $padrao = Array(
            'fieldLabel' => isset($setup['label']) ? $setup['label'] : $setup['name'],
            'extXtype' => 'combo',
            'extTransform' => 'stateSelect',
            'extForceSelection' => true,
            'extQueryMode' => 'local',
            'extDisplayField' => 'value',
            'extValueField' => 'key',
            'extStore' => "{xtype:'store', fields : [ 'key','value' ],data : $options }"
        );

        return self::standField($setup, $padrao);
    }

}
